package com.moqianfan.mcuassistant.fragment.learn;


import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.moqianfan.mcuassistant.R;
import com.moqianfan.mcuassistant.adapter.BaseFragmentAdapter;
import com.moqianfan.mcuassistant.fragment.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/11/5
 * 学习.
 */

public class LearnFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {
    @BindView(R.id.vp_learn)
    ViewPager vpLearn;
    @BindView(R.id.vp_play_top)
    RadioGroup radioGLearn;
    @BindView(R.id.radio_learn_51)
    RadioButton radioBtnMcu;
    @BindView(R.id.radio_learn_c)
    RadioButton radioBtnCG;

    @Override
    public View findViews(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_learn, container, false);
    }

    @Override
    public void initViews() {
        List<BaseFragment> fragments = new ArrayList<>();
        fragments.add(new LearnMcuFragment());
        fragments.add(new LearnCgramFragment());
        vpLearn.setAdapter(new BaseFragmentAdapter(getChildFragmentManager(), fragments));
        vpLearn.setCurrentItem(0);
    }


    @Override
    public void loadData() {
        vpLearn.addOnPageChangeListener(this);
        radioGLearn.setOnCheckedChangeListener(this);
    }

    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.radio_learn_51:
                vpLearn.setCurrentItem(0);
                break;
            case R.id.radio_learn_c:
                vpLearn.setCurrentItem(1);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    public void onPageScrollStateChanged(int state) {
        if (state == 2) {
            switch (vpLearn.getCurrentItem()) {
                case 0:
                    radioBtnMcu.setChecked(true);
                    break;
                case 1:
                    radioBtnCG.setChecked(true);
                    break;
            }
        }
    }
}
